﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using XiaoYuStock.Core.Entities;
namespace XiaoYuStock.Core
{
   public class HistoryDataService
    {
        /// <summary>
        /// 
        /// http://quotes.money.163.com/trade/lsjysj_600035.html?year=2019&season=4
        /// </summary>
        private static string requestUrl = "http://quotes.money.163.com/trade/lsjysj_{0}.html?year={1}&season={2}";

       private static Regex regTable = new Regex("<table\\s*class=\"table_bg001 border_box limit_sale\">.*?</table>", RegexOptions.IgnoreCase|RegexOptions.Singleline);

       private static Regex regTR = new Regex("<tr.*?</tr>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
       private static Regex regTD = new Regex("<td.*?</td>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
       /// <summary>
       /// 股票代码
       /// </summary>
       public string SharesCode { get; set; }

       /// <summary>
       /// 年
       /// </summary>
       public string Year { get; set; }

       /// <summary>
       /// 季度
       /// </summary>
       public int Season { get; set; }

       public List<TransferData> GetDataRange()
       {
           List<TransferData> list = new List<TransferData>();
           var url = string.Format(requestUrl, this.SharesCode, this.Year, this.Season);
           var htmlContent = RequestHelp.Get(url, Encoding.GetEncoding("utf-8"));
           if (!string.IsNullOrWhiteSpace(htmlContent))
           {
               string tableHtml = regTable.Match(htmlContent).Value;
               if (!string.IsNullOrWhiteSpace(tableHtml))
               {
                   MatchCollection mc = regTR.Matches(tableHtml);
                   if (mc != null)
                   {
                       for (int i = 2; i < mc.Count;i++ )
                       {
                           TransferData item = new TransferData();
                           var td = regTD.Matches(mc[i].Value);
                           if (td != null)
                           {
                                //日期 开盘价 最高价 最低价 收盘价 涨跌额 涨跌幅(%)  成交量(手)  成交金额(万元)    振幅(%)   换手率(%)
                               item.Date = RequestHelp.striphtml(td[0].Value).Trim();
                               item.TodayOpenPrice =Convert.ToDecimal(RequestHelp.striphtml(td[1].Value));
                               item.TodayTopPrice = Convert.ToDecimal(RequestHelp.striphtml(td[2].Value));
                               
                               item.TodayBomPrice = Convert.ToDecimal(RequestHelp.striphtml(td[3].Value));
                                item.TodayClosePrice = Convert.ToDecimal(RequestHelp.striphtml(td[4].Value));
                                item.TradeCount = Convert.ToInt64(RequestHelp.striphtml(td[8].Value).Replace(",",""));
                               item.TradeAmount = Convert.ToDecimal(RequestHelp.striphtml(td[9].Value).Replace(",", ""));
                               item.SharesCode = SharesCode;
                               list.Add(item);
                           }
                       }
                   }
               }
           }
           return list;
       }
   }
}
